package com.vca.service.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vca.common.exception.VcaException;
import com.vca.common.model.about.VcaAbout;
import com.vca.common.request.VcaAboutAddOrUpdateRequest;
import com.vca.common.request.VcaAboutAdminRequest;
import com.vca.common.response.AboutAdminResponse;
import com.vca.common.response.AboutFrontResponse;
import com.vca.common.response.VcaAboutAdminResponse;
import com.vca.service.dao.about.VcaAboutDao;
import com.vca.service.service.SystemAttachmentService;
import com.vca.service.service.SystemGroupDataService;
import com.vca.service.service.VcaAboutService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * 关于我们表 实现类
 *
 * @author Li
 * @since 2022/11/28 11:20
 */
@Service
public class VcaAboutServiceImpl extends ServiceImpl<VcaAboutDao, VcaAbout> implements VcaAboutService {

    @Resource
    private VcaAboutDao dao;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    @Autowired
    private SystemGroupDataService systemGroupDataService;


    /**
     * 关于我们列表
     *
     * @return
     * @author Li
     * @date 2022/11/28 11:29
     */
    @Override
    public List<VcaAboutAdminResponse> getList(VcaAboutAdminRequest request) {
        List<VcaAboutAdminResponse> responses = new ArrayList<>();
        LambdaQueryWrapper<VcaAbout> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VcaAbout::getIsDeleted, 0);
        if (ObjectUtil.isNotEmpty(request.getName())) {
            wrapper.like(VcaAbout::getName, request.getName()).or().like(VcaAbout::getNameEn, request.getName());
        }
        if (ObjectUtil.isNotEmpty(request.getStatus())) {
            wrapper.eq(VcaAbout::getStatus, request.getStatus());
        }
        wrapper.orderByDesc(VcaAbout::getSort);
        List<VcaAbout> vcaAbouts = dao.selectList(wrapper);
        vcaAbouts.forEach(e -> {
            VcaAboutAdminResponse response = new VcaAboutAdminResponse();
            BeanUtils.copyProperties(e, response);
            response.setDetails(e.getDetails());
            responses.add(response);
        });
        return responses;
    }

    /**
     * 修改排序
     *
     * @param aboutId
     * @param sort
     * @return
     * @author Li
     * @date 2022/11/28   13:10
     */
    @Override
    public boolean updateSort(Long aboutId, Integer sort) {
        VcaAbout vcaAbout = checkAbout(aboutId);
        vcaAbout.setSort(sort);
        return Boolean.TRUE.equals(transactionTemplate.execute(e -> {
            dao.updateById(vcaAbout);
            return Boolean.TRUE;
        }));
    }

    /**
     * @param aboutId
     * @Description: 修改状态
     * @Param: [aboutId]
     * @author: Li
     * @date: 2022/11/28 13:45
     * @return: boolean
     */
    @Override
    public boolean updateStatus(Long aboutId) {
        VcaAbout vcaAbout = checkAbout(aboutId);
        vcaAbout.setStatus(vcaAbout.getStatus() == 1 ? 0 : 1);
        return Boolean.TRUE.equals(transactionTemplate.execute(e -> {
            dao.updateById(vcaAbout);
            return Boolean.TRUE;
        }));
    }

    /**
     * @param request
     * @return boolean
     * @description 修改关于我们
     * @author Li
     * @date 2022/11/28 13:51
     */
    @Override
    public boolean updateAbout(VcaAboutAddOrUpdateRequest request) {
        VcaAbout vcaAbout = new VcaAbout();
        BeanUtils.copyProperties(request, vcaAbout);
        List<String> carouselMap = new ArrayList<String>();
        request.getCarouselMap().forEach(e -> {
            carouselMap.add(systemAttachmentService.clearPrefix(e));
        });

        vcaAbout.setBackgroundImage(systemAttachmentService.clearPrefix(vcaAbout.getBackgroundImage()));
        vcaAbout.setCoverImage(systemAttachmentService.clearPrefix(vcaAbout.getCoverImage()));
        vcaAbout.setMainPictureVideo(systemAttachmentService.clearPrefix(vcaAbout.getMainPictureVideo()));
        String jsonString = JSONArray.parseArray(JSON.toJSONString(carouselMap)).toJSONString();
        vcaAbout.setCarouselMap(jsonString);
        vcaAbout.setDetails(systemAttachmentService.clearPrefix(request.getDetails()));
        vcaAbout.setDetailsEn(systemAttachmentService.clearPrefix(request.getDetailsEn()));
        return Boolean.TRUE.equals(transactionTemplate.execute(e -> {
            dao.updateById(vcaAbout);
            return Boolean.TRUE;
        }));
    }

    /**
     * @param request
     * @return boolean
     * @description 添加关于我们
     * @author Li
     * @date 2022/11/28 13:55
     */
    @Override
    public boolean addAbout(VcaAboutAddOrUpdateRequest request) {
        VcaAbout vcaAbout = new VcaAbout();
        BeanUtils.copyProperties(request, vcaAbout);
        List<String> carouselMap = new ArrayList<String>();
        request.getCarouselMap().forEach(e -> {
            carouselMap.add(systemAttachmentService.clearPrefix(e));
        });

        vcaAbout.setBackgroundImage(systemAttachmentService.clearPrefix(vcaAbout.getBackgroundImage()));
        vcaAbout.setCoverImage(systemAttachmentService.clearPrefix(vcaAbout.getCoverImage()));
        vcaAbout.setMainPictureVideo(systemAttachmentService.clearPrefix(vcaAbout.getMainPictureVideo()));
        String jsonString = JSONArray.parseArray(JSON.toJSONString(carouselMap)).toJSONString();
        vcaAbout.setCarouselMap(jsonString);
        vcaAbout.setDetails(systemAttachmentService.clearPrefix(request.getDetails()));
        vcaAbout.setDetailsEn(systemAttachmentService.clearPrefix(request.getDetailsEn()));
        return Boolean.TRUE.equals(transactionTemplate.execute(e -> {
            dao.insert(vcaAbout);
            return Boolean.TRUE;
        }));
    }

    /**
     * @param aboutId
     * @return boolean
     * @description 删除
     * @author Li
     * @date 2022/11/28 14:13
     */
    @Override
    public boolean deleteAbout(Long aboutId) {
        VcaAbout vcaAbout = checkAbout(aboutId);
        vcaAbout.setIsDeleted(1);
        return Boolean.TRUE.equals(transactionTemplate.execute(e -> {
            dao.update(vcaAbout, new LambdaUpdateWrapper<VcaAbout>().eq(VcaAbout::getId, aboutId).set(VcaAbout::getIsDeleted, 1));
            return Boolean.TRUE;
        }));
    }

    /**
     * @param aboutId
     * @return com.vca.common.response.AboutAdminResponse
     * @description 根据id查询详情
     * @author Li
     * @date 2022/12/2 18:21
     */
    @Override
    public AboutAdminResponse getAbout(Long aboutId) {
        VcaAbout vcaAbout = checkAbout(aboutId);
        AboutAdminResponse response = new AboutAdminResponse();
        response.setDetails(JSONArray.parseArray(vcaAbout.getDetails()));
        response.setDetailsEn(JSONArray.parseArray(vcaAbout.getDetailsEn()));
        BeanUtils.copyProperties(vcaAbout, response);
        return response;
    }

    /**
     * @return java.util.List<com.vca.common.response.AboutFrontResponse>
     * @description 获取关于列表
     * @author Li
     * @date 2022/12/9 14:44
     */
    @Override
    public List<AboutFrontResponse> getFrontList(String language) {
        LambdaQueryWrapper<VcaAbout> wrapper = new LambdaQueryWrapper();
        wrapper.eq(VcaAbout::getStatus, 1);
        wrapper.eq(VcaAbout::getIsDeleted, 0);
        List<VcaAbout> vcaAbouts = dao.selectList(wrapper);
        List<AboutFrontResponse> responses = new ArrayList<>();
        vcaAbouts.forEach(e -> {
            AboutFrontResponse response = new AboutFrontResponse();
            BeanUtils.copyProperties(e, response);
            if("en".equals(language)){
                response.setTitle(e.getNameEn());
                response.setIntroduction(e.getIntroductionEn());
                response.setDetails(JSON.parseArray(e.getDetailsEn()));
            }else{
                response.setTitle(e.getName());
                response.setIntroduction(e.getIntroduction());
                response.setDetails(JSON.parseArray(e.getDetails()));
            }
            response.setThumbnail(e.getCoverImage());
            response.setCarouselMap(JSON.parseArray(e.getCarouselMap()));
            responses.add(response);
        });
        responses.sort(Comparator.comparing(AboutFrontResponse::getSort).reversed());
        return responses;
    }

    /**
     * @param id
     * @return {@link AboutFrontResponse}
     * @description 根据id获取关于信息
     * @methodName getFrontAbout
     * @author Li
     * @date 2023/1/9 9:28
     */
    @Override
    public AboutFrontResponse getFrontAbout(String language,Long id) {
        VcaAbout vcaAbout = checkAbout(id);
        AboutFrontResponse response = new AboutFrontResponse();
        BeanUtils.copyProperties(vcaAbout, response);
        if("en".equals(language)){
            response.setTitle(vcaAbout.getNameEn());
            response.setDetails(JSON.parseArray(vcaAbout.getDetailsEn()));
        }else{
            response.setTitle(vcaAbout.getName());
            response.setDetails(JSON.parseArray(vcaAbout.getDetails()));
        }
        response.setThumbnail(vcaAbout.getCoverImage());
        response.setCarouselMap(JSON.parseArray(vcaAbout.getCarouselMap()));
        return response;
    }

    /**
     * @return {@link Map}<{@link String,Object}>
     * @description 获取关于列表和配置信息
     * @methodName getFrontAboutAndConfig
     * * @param
     * @author Li
     * @date 2023/1/9 11:18
     */
    @Override
    public Map<String, Object> getFrontAboutAndConfig(String language) {
        Map<String, Object> aboutList = systemGroupDataService.getAboutList(language);
        aboutList.put("list", getFrontList(language));
        return aboutList;
    }

    /**
     * @Description: 根据id获取信息
     * @Param: [aboutId]
     * @author: Li
     * @date: 2022/11/28 13:41
     * @return: com.vca.common.model.about.VcaAbout
     */
    private VcaAbout checkAbout(Long aboutId) {
        LambdaQueryWrapper<VcaAbout> wrapper = new LambdaQueryWrapper();
        wrapper.eq(VcaAbout::getId, aboutId);
        wrapper.eq(VcaAbout::getIsDeleted, 0);
        VcaAbout vcaAbout = dao.selectOne(wrapper);
        if (ObjectUtil.isEmpty(vcaAbout)) {
            throw new VcaException("不存在");
        }
        return vcaAbout;
    }
}
